#region License and Copyright

/*
 * Copyright (C) 2008 Nathan Palmer
 * http://www.nathanpalmer.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author:
 *     Nathan Palmer <nathan1@nathanpalmer.com>
 */

#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Amazon.S3.com.amazonaws.s3;
using System.IO;

namespace Amazon.S3
{
    public class S3Key
    {
        private ListEntry entry;
        private S3Helper helper;
        private S3Bucket bucket;
        private S3MetadataCollection metaData;

        internal S3Key(ListEntry Entry, S3Bucket Bucket, S3Helper Helper)
        {
            entry = Entry;
            helper = Helper;
            bucket = Bucket;
        }

        public string Name
        {
            get
            {
                return entry.Key;                
            }
        }

        public long Size
        {
            get
            {
                return entry.Size;
            }
        }

        public DateTime LastModified
        {
            get
            {
                return entry.LastModified;
            }
        }

        private bool LoadMetadata()
        {
            SoapResponse<GetObjectResult> soapResult;
            if (helper.GetObjectMetadata(bucket.Name, Name, out soapResult))
            {
                metaData = new S3MetadataCollection(soapResult.State.Metadata);
                return true;
            }
            else
            {
                return false;
            }
        }

        public S3MetadataCollection Metadata
        {
            get
            {
                if (metaData == null)
                {
                    if (LoadMetadata())
                    {
                        return metaData;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return metaData;
                }
            }
        }

        public FileInfo Save(string FilePath)
        {
            byte[] data = new byte[0];
            SoapResponse<GetObjectResult> soapResponse;
            if (helper.GetObject(bucket.Name, this.Name, ref data, out soapResponse))
            {
                string destinationPath = Path.Combine(FilePath, this.Name);
                FileInfo fileObject = new FileInfo(destinationPath);

                FileStream fs = fileObject.Open(FileMode.CreateNew);
                fs.Write(data, 0, data.Length);
                fs.Close();

                fileObject.Refresh();

                return fileObject;
            }
            else
            {
                return null;
            }
        }

        public byte[] GetData()
        {
            byte[] data = new byte[0];
            SoapResponse<GetObjectResult> soapResponse;
            if (helper.GetObject(bucket.Name, this.Name, ref data, out soapResponse))
            {
                return data;
            }
            else
            {
                return null;
            }
        }

        public byte[] GetDataProgressive(long ByteStart, long ByteLength)
        {
            byte[] data = new byte[0];
            SoapResponse<GetObjectResult> soapResponse;
            if (helper.GetObjectExtended(bucket.Name, this.Name, ByteStart, (ByteStart + ByteLength - 1), ref data, out soapResponse))
            {
                return data;
            }
            else
            {
                return null;
            }
        }
    }
}
